<template>
  <div class="app-container" v-loading="loading" v-if="true">
    <div class="filter-container">
      <el-input
        style="width: 200px"
        type="name"
        class="filter-item"
        placeholder="商户名称"
        v-model="queryList.name" @keyup.enter.native="search"></el-input>
      <el-button type="primary" icon="el-icon-search" class="filter-item" @click="search">搜索</el-button>
      <el-button class="filter-item" @click="reset">重置</el-button>
    </div>
    <!--功能按钮-->
    <div class="action-container">
      <el-button type="primary" icon="el-icon-plus" size="small" @click="openAddDialog">创建商户</el-button>
      <el-button icon="el-icon-refresh" size="small" @click="refresh">刷新</el-button>
    </div>
    <!--表格数据-->
    <el-table
      :data="tableData"
      border
      style="width: 100%">
      <el-table-column
        type="selection"
        width="55">
      </el-table-column>
      <el-table-column
        prop="account"
        label="账号">
      </el-table-column>
      <el-table-column
        prop="name"
        label="商户名称">
      </el-table-column>
      <el-table-column
        prop="businessLicense"
        label="营业执照">
      </el-table-column>
      <el-table-column
        prop="legalPerson"
        label="法人">
      </el-table-column>
      <el-table-column
        prop="createTime"
        label="创建时间">
      </el-table-column>
      <el-table-column
        label="操作"
        align="left" max-width="30">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="primary"
            @click="openEditDialog(scope.row)">编辑
          </el-button>
          <el-button
            size="mini"
            type="primary"
            @click="detail(scope.row.id)">详情
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!--分页-->
    <pagination
      :total-elements="totalElements"
      :change-callback="getTableList"
      ref="page">
    </pagination>
    <!--创建商户-->
    <el-dialog title="创建商户" :visible.sync="addDialog">
      <el-form :model="form">
        <el-form-item label="营业执照" :label-width="formLabelWidth">
          <el-input v-model="form.businessLicense" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="法人" :label-width="formLabelWidth">
          <el-input v-model="form.legalPerson" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="商户名称" :label-width="formLabelWidth">
          <el-input v-model="form.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="管理账号" :label-width="formLabelWidth">
          <el-input v-model="form.adminAccount" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="closeAddDialog">取 消</el-button>
        <el-button type="primary" @click="addMerchant">确 定</el-button>
      </div>
    </el-dialog>
    <!--修改商户-->
    <el-dialog title="修改商户" :visible.sync="editDialog">
      <el-form :model="form">
        <el-form-item label="营业执照" :label-width="formLabelWidth">
          <el-input v-model="form.businessLicense" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="法人" :label-width="formLabelWidth">
          <el-input v-model="form.legalPerson" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="商户名称" :label-width="formLabelWidth">
          <el-input v-model="form.name" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="editDialog = false">取 消</el-button>
        <el-button type="primary" @click="editMerchant">确 定</el-button>
      </div>
    </el-dialog>

    <!--商户详情-->
    <el-dialog title="商户详情" :visible.sync="dialogTableVisible">
      <el-table :data="detailData">
        <el-table-column
        prop="name"
        label="门店名称">
        </el-table-column>
        <el-table-column
          prop="address"
          label="门店地址">
        </el-table-column>
        <el-table-column
          prop="username"
          label="店长账号">
        </el-table-column>
        <el-table-column
          prop="createTime"
          label="创建时间">
        </el-table-column>
      </el-table>
    </el-dialog>
  </div>


</template>

<script>
  import {getTableList, addMerchant,findMerchantId, editMerchant, findStoreInfo} from '@/modules/merchant/api/merchant'
  import pagination from '@/components/pagination/index'

  export default {
    name: 'merchant',
    data() {
      return {
        addDialog: false,
        tableData: [], // 表格数据
        totalElements: 0, // 数据条数
        multipleSelection: [], // 选中数据-多选
        editDialog: false, // 编辑模态框
        loading: true, // loading效果
        formLabelWidth: '120px',//设置宽度
        btnLoading: false, // 按钮loading
        //创建模态框src
        createdSrc: '',
        //编辑模态框src
        editSrc: '',
        dialogTableVisible: false,
        editName: '',
        form: {
          id: '',
          businessLicense: '',
          legalPerson: '',
          name: '',
          adminAccount: '',
          username: '',
        },
        queryList: {
          name: '',
        },
        options: [],
        detailData: [], // 详情数据
        headers: {
          accessToken: sessionStorage.token
        }
      }
    },
    components: {pagination},
    created() {
      this.getTableList()
    },
    methods: {
      // 打开创建商户框
      openAddDialog() {
        this.addDialog = true;
        this.form = {
          businessLicense: '',
          legalPerson: '',
          name: '',
          adminAccount: '',
          username: '',
          password: '',
        }
      },
      openEditDialog(row) {
        this.form.id = row.id
        this.form.businessLicense = row.businessLicense
        this.form.legalPerson = row.legalPerson
        this.form.name = row.name
        this.form.adminAccount = row.adminAccount
        this.editDialog = true
      },
      //表格列表
      getTableList: function (number, pageSize) {
        this.loading = true
        getTableList(number, pageSize,this.queryList).then(response => {
          let data = response.obj.content
          this.tableData = data
          this.totalElements = response.obj.totalElements
          this.loading = false
        }).catch(() => {
          this.loading = false
        })
      },
      /*
     * 关闭添加用户模态框
     * */
      closeAddDialog() {
        this.addDialog = false
        this.form = {
          password: '',
          name: '',
          phone: '',
          email: '',
          merchantId: ''
        }
      },
      /*
     * 创建商户
     * */
      addMerchant() {
        this.loading = true
        this.btnLoading = true
        addMerchant(this.form).then(response => {
          this.$message({
            message: response.msg,
            type: 'success'
          })
          this.btnLoading = false
          this.closeAddDialog();
          this.$refs.page.refresh()
        }).catch(() => {
          this.btnLoading = false
          this.loading = false
        })
      },
      /*
     * 修改商户
     * */
      editMerchant() {
        this.loading = true
        this.btnLoading = true
        editMerchant(this.form).then(response => {
          this.$message({
            message: response.msg,
            type: 'success'
          })
          this.btnLoading = false
          this.editDialog = false
          this.$refs.page.refresh()
        }).catch(() => {
          this.btnLoading = false
          this.loading = false
        })
      },

      /**
       * 查看门店详情，跳转页面
       */
      detail(merchantId) {
        // this.$router.push({path:'/merchant/views/merchant_detail',query: {id: merchantId}});
        findStoreInfo(merchantId).then(response => {
            this.detailData = response.obj
          this.dialogTableVisible = true
        }).catch(() => {
          this.btnLoading = false
          this.loading = false
        })
      },

      /**
       * 重置
       */
      reset () {
        this.queryList = {
          name: '',
        }
        this.$refs.page.search()
      },
      /**
       * 搜索
       */
      search () {
        this.$refs.page.search()
      },

      /*
       * 刷新
       * */
      refresh() {
        this.$refs.page.refresh()
      },
    }

  }
</script>

<style scoped>
  .logo-uploader {
    display: inline-block;
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }

  .logo-uploader:hover {
    border-color: #409EFF;
  }

  .logo-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }

  .logo {
    width: 178px;
    height: 178px;
    display: block;
  }
</style>
